package com.sikaryofficial.workbench.utils.excel.converter.goods;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ReadConverterContext;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.sikaryofficial.workbench.domain.eo.GoodsTasteEO;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GoodsTasteConverter implements Converter<List<GoodsTasteEO>> {

    /**
     * 全局的分隔符
     */
    private static final String SEPARATOR = "[;；]";

    private static final String LINK_SYMBOL = "_";
    /**
     * 匹配口味字符串.eg：
     * 口味1_N00023_#254321_100
     */
    private static final Pattern PATTERN = Pattern.compile("([^-]+)" + LINK_SYMBOL + "([^-]+)" + LINK_SYMBOL + "\\s*(#[0-9A-Fa-f]{6})\\s*" + LINK_SYMBOL + "\\s*(\\d*)\\s*");

    @Override
    public Class<?> supportJavaTypeKey() {
        return List.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public List<GoodsTasteEO> convertToJavaData(ReadConverterContext<?> context) throws Exception {
        ReadCellData<?> readCellData = context.getReadCellData();
        String stringValue = readCellData.getStringValue();

        List<GoodsTasteEO> goodsTasteEOList = new ArrayList<>();
        String[] split = stringValue.split(SEPARATOR);
        for (String tasteStr : split) {
            if (tasteStr == null || tasteStr.trim().isEmpty()) {
                continue;
            }
            Matcher matcher = PATTERN.matcher(tasteStr);
            if (matcher.matches()) {
                String tasteName = matcher.group(1).trim();
                String productNo = matcher.group(2).trim();
                String goodsColor = matcher.group(3).trim();
                String minQuantityStr = matcher.group(4).trim();

                GoodsTasteEO goodsTasteEO = new GoodsTasteEO();
                goodsTasteEOList.add(goodsTasteEO);
                goodsTasteEO.setTasteName(tasteName);
                goodsTasteEO.setProductNo(productNo);
                goodsTasteEO.setGoodsColor(goodsColor);
                goodsTasteEO.setMinQuantity(Integer.parseInt(minQuantityStr));
            } else {
                throw new IllegalArgumentException("口味解析失败：" + tasteStr);
            }
        }
        return goodsTasteEOList;
    }

    @Override
    public WriteCellData<?> convertToExcelData(WriteConverterContext<List<GoodsTasteEO>> context) throws Exception {
        List<GoodsTasteEO> goodsTasteEOList = context.getValue();
        if (goodsTasteEOList == null || goodsTasteEOList.isEmpty()) {
            return new WriteCellData<>("");
        }

        StringBuilder sb = new StringBuilder();
        for (GoodsTasteEO goodsTasteEO : goodsTasteEOList) {
            sb.append(Objects.toString(goodsTasteEO.getTasteName(), ""))
                    .append(LINK_SYMBOL).append(Objects.toString(goodsTasteEO.getProductNo(), ""))
                    .append(LINK_SYMBOL).append(Objects.toString(goodsTasteEO.getGoodsColor(), ""))
                    .append(";");
        }
        return new WriteCellData<>(sb.toString());
    }
}
